import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:数组的创建和使用
 * User: liangyuanzhang
 * Date: 2024-12-04
 * Time: 22:06
 */
public class Work {
    /*
    数字是连续出现的，只需要定义一个计数器，
    如果连续出现的次数超过3，则返回true。
     */
/*    public static boolean isConsecutiveOdd(int n){
        return n % 2 != 0;
    }
    public static boolean threeConsecutiveOdds(int[] array){
        //引入标记位记录连续出现奇数的个数
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (isConsecutiveOdd(array[i])){
                count++;
            }
            if (count == 3){//当count == 3 说明已经连续出现三个奇数
                return true;
            }
        }
        return false;
    }*/
    public static boolean func(int[] array){
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if(array[i] % 2 != 0){
                count++;
                if(count == 3){
                    return true;
                }
            }else {
                count = 0;
            }
        }
        return false;
    }
    public static void main(String[] args) {
        int[] array = {1,2,34,3,4,5,7,23,12};
        boolean ret = func(array);
        System.out.println(ret);
    }
    /**
     * 多数元素
     * 排序法
     * 将数组排序后，数组n/2的元素一定是众数
     */
    public static int maiorityElement(int[] array){
        Arrays.sort(array);
        return array[array.length/2];
    }
    public static void main7(String[] args) {
        int[] array = {3,2,3};
        int ret = maiorityElement(array);
        System.out.println(ret);
    }
    //只出现一次的数字
    public static int singleNumber(int[] array){
        int ret = 0;
        for (int i:array) {
            ret ^= i;//异或的时候两个数字相同 == 0
        }
        return ret;
    }
    public static void main6(String[] args) {
        int[] array = {2,2,1};
        int ret = singleNumber(array);
        System.out.println(ret);
    }
    //两数之和
    public static int[] towSum(int[] array,int target){
        int[] result = new int[2];
        //双指针i和j，i从前往后遍历，j从后往前遍历，若array[i] + array[j] == target,即为解
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - 1; j++) {
                if(array[i] + array[j] == target){
                    result[0] = i;
                    result[1] = j;
                }
            }
        }
        return result;
    }
    public static void main5(String[] args) {
        int[] array = {2,7,11,15};
        int[] ret = towSum(array,9);
        System.out.println(Arrays.toString(ret));
    }
    //冒泡排序
    public static void bubbleSort(int[] array){
        //i 代表趟数
        for (int i = 0; i < array.length; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length - 1-i; j++) {
                if(array[j] >array[j+1]){
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                    flg = true;
                }
            }
            if(flg == false){
                return;
            }
        }
    }
    public static void main4(String[] args) {
        int[] array = {1,2,31,4,15};
        System.out.println("排序前的数组:"+Arrays.toString(array));
        bubbleSort(array);
        System.out.println("===========");
        System.out.println("排序后的数组:"+Arrays.toString(array));
    }
    public static void main3(String[] args) {
        int[] array = {1,2,31,4,15};
        Arrays.sort(array);//先排序
        System.out.println("排序后的数组:"+Arrays.toString(array));
        //排序完成进行二分查找
        int ret = Arrays.binarySearch(array,2);//实现二分查找 --> 前提是数组是有序的
        System.out.println("下标为:"+ret);
    }
    //奇数位于偶数之前
    /*
    不用i、j，可以用left 和 right
     */
    public static int[] reorderArray(int[] array){
        int i = 0;
        int j = array.length-1;
        while(i < j){
            while (i < j && array[i] % 2!= 0){
                i++;
            }
            while (i < j && array[j] % 2 == 0){
                j--;
            }
            if(i <j){
                int tmp = array[i];
                array[i] = array[j];
                array[j] = tmp;
            }
        }
        return array;
    }
    public static void main2(String[] args) {
        int[] array = {1,2,3,4,5,6};
        int[] ret = reorderArray(array);
        System.out.println(Arrays.toString(ret));
    }
    //改变原有数组元素的值
    public static int[] transform(int[] array){
        int[] tmp = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            tmp[i] = array[i]*2;
        }
        return tmp;
    }
    public static void main1(String[] args) {
        int[] array = {1,2,3};
        int[] ret = transform(array);
        System.out.println(Arrays.toString(ret));
    }
}
